package com.vtquang.mymaps.overlay;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Paint.Style;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.vtquang.mymaps.R;

public class MyLocationOverlay extends LocationOverlay {
	private int rong = 70;
	private int cao = 70;
	private double _angle = 0; 
	private float position = 0;

	public MyLocationOverlay(GeoPoint gp, Context context) {
		super(gp, context);
	}

	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {
		_point = new Point();
		mapView.getProjection().toPixels(_gp, _point);

		int with = _bmp.getWidth();
		int height = _bmp.getHeight();
		
		int left = (int) (_point.x - with / 2);
		int top = (int) (_point.y - height / 2); 

		Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

		circlePaint.setColor(0x0F009AFF);
		circlePaint.setStyle(Style.FILL_AND_STROKE); 
		canvas.drawOval(new RectF(_point.x - rong, _point.y - cao, _point.x
				+ rong, _point.y + cao), circlePaint);

		circlePaint.setColor(0x79009AFF);
		circlePaint.setStyle(Style.STROKE); 
		canvas.drawOval(new RectF(_point.x - rong, _point.y - cao, _point.x
				+ rong, _point.y + cao), circlePaint); 
		 
		Matrix matrix = new Matrix();
		matrix.setRotate((float) -_angle, with / 2, height / 2);
		Bitmap newbmp = Bitmap.createBitmap(_bmp, 0, 0, with, height, matrix,
				true);
		canvas.drawBitmap(newbmp, left, top, null);
//		
//		
//		Paint paint = new Paint();
//		paint.setStyle(Paint.Style.FILL);
//		paint.setColor(Color.RED);
//		paint.setTextSize(20);
//
//		paint = new Paint();
//		paint.setAntiAlias(true);
//		paint.setStrokeWidth(2);
//		paint.setTextSize(25);
//		paint.setStyle(Paint.Style.STROKE);
//		paint.setColor(Color.WHITE);
//
//		float x = (float) (_point.x + rong
//				* Math.sin((double) (position) / 180 * 3.143));
//		float y = (float) (_point.y - rong
//				* Math.cos((double) (position) / 180 * 3.143));
//
//		x = 2 * _point.x - x;
//		canvas.drawLine(_point.x, _point.y, x, y, paint);
		return true;
	}

	@Override
	protected void getBitmap() {
		Resources res = _context.getResources();
		_bmp = BitmapFactory.decodeResource(res, R.drawable.ic_pin_mylocation);
	}

	public static String getNameOverlay() {
		return "MyLocationOverlay";
	}

	@Override
	public String getName() {
		return getNameOverlay();
	}

	public void setAngle(double angle) {
		this._angle = angle;
	}

	public void setGeoPoint(GeoPoint gp) {
		this._gp = gp;
	}

	public void updateData(float angle) {
		this._angle = angle + 90;
	}
}
